﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WebSiteAdminTool.UI.ViewModels.Base;
using WebSiteAdminTool.UI.Utils.Commands;
using WebSiteAdminTool.UI.Services;
using WebSiteAdminTool.UI.Views.Security;
using WebSiteAdminTool.UI.ViewModels.Security;
using WebSiteAdminTool.UI.Services.Navigation;
using WebSiteAdminTool.UI.Ioc;
using WebSiteAdminTool.UI.RolesService;
using WebSiteAdminTool.UI.ViewModels.Security.Roles;
using WebSiteAdminTool.UI.Services.Messaging;
using WebSiteAdminTool.UI.Services.Messaging.DataManipulationMessages;

namespace WebSiteAdminTool.UI.ViewModels
{
    public class RoleManagementActionsViewModel : BaseNavigableViewModel
    {
        #region private data

        private RoleData _currentRole = null;
        // commands
        private GenericCommand _enableRolesCommand;
        private GenericCommand _disableRolesCommand;
        private GenericCommand _createRoleCommand;
        private GenericCommand _editRoleCommand;
        private GenericCommand _removeRoleCommand;
        private GenericCommand _addRemoveUsersCommand;

        #endregion

        #region ctors

        public RoleManagementActionsViewModel()
        {
            base.InitViewModel();
        }

        #endregion

        #region overriden base methods

        protected override void InitializeCommands()
        {
            _enableRolesCommand = new ActionLinkClickedCommand(this.EnableRoles);
            _disableRolesCommand = new ActionLinkClickedCommand(this.DisableRoles);
            _createRoleCommand = new ActionLinkClickedCommand(this.CreateRole, true);
            _editRoleCommand = new ActionLinkClickedCommand(this.EditRole);
            _removeRoleCommand = new ActionLinkClickedCommand(this.RemoveRole);
            _addRemoveUsersCommand = new ActionLinkClickedCommand(this.AddRemoveUsers);

            base.InitializeCommands();
        }

        protected override void RegisterListeners()
        {
            MessageManager.RegisterListener<SelectedItemChangedMessage<RoleData>>(
                this, message => CurentRole = message.Content);

            base.RegisterListeners();
        }

        #endregion

        #region public props

        private RoleData CurentRole
        {
            get { return _currentRole; }
            set
            {
                _currentRole = value;
                EditRoleCommand.IsEnabled =
                    RemoveRoleCommand.IsEnabled =
                    AddRemoveUsersCommand.IsEnabled = null != _currentRole;
            }
        }

        public GenericCommand EnableRolesCommand { get { return _enableRolesCommand; } }
        public GenericCommand DisableRolesCommand { get { return _disableRolesCommand; } }
        public GenericCommand CreateRoleCommand { get { return _createRoleCommand; } }
        public GenericCommand EditRoleCommand { get { return _editRoleCommand; } }
        public GenericCommand RemoveRoleCommand { get { return _removeRoleCommand; } }
        public GenericCommand AddRemoveUsersCommand { get { return _addRemoveUsersCommand; } }

        #endregion

        #region command event handlers

        private void EnableRoles(object data)
        {
            throw new NotImplementedException();
        }

        private void DisableRoles(object data)
        {
            throw new NotImplementedException();
        }

        private void CreateRole(object data)
        {
            SecurityUiHelpers.ManageRoleDialog();
        }

        private void EditRole(object data)
        {
            if (null != this.CurentRole)
            {
                SecurityUiHelpers.ManageRoleDialog(this.CurentRole);
            }
        }

        private void RemoveRole(object data)
        {
            SecurityUiHelpers.RemoveRole(this.CurentRole);
        }

        private void AddRemoveUsers(object data)
        {
            SecurityUiHelpers.ManageRoleUsers(this.CurentRole);
        }

        #endregion
    }
}
